﻿using System;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Olitee.WorkScheduler.Tests.Model;

namespace Olitee.WorkScheduler.Tests.SchedulerTests
{
    [TestClass]
    public class WhenUsingTheScheduler
    {
        [TestMethod]
        public void TheSchedulerStarts()
        {
            var s = new Scheduler();
            s.RegisterWorkProcessor<TestWorkItem, TestWorkProcessor>();

            var item = new TestWorkItem();
            s.AddWorkItem(item);
            s.StartScheduler();

            // Now wait a moment for the scheduler to spin up and process the work.
            Thread.Sleep(1000);

            Assert.IsTrue(item.Status == Enumerations.WorkItemStatus.Complete);
        }

        [TestMethod]
        public void TheSchedulerCanBeStopped()
        {
            var s = new Scheduler();
            s.RegisterWorkProcessor<TestWorkItem, TestWorkProcessor>();

            var item1 = new TestWorkItem();
            var item2 = new TestWorkItem();
            s.AddWorkItem(item1);
            s.StartScheduler();

            // Now wait a moment for the scheduler to spin up and process the work.
            Thread.Sleep(100);

            // Now we stop the scheduler
            s.StopScheduler();

            Thread.Sleep(100);
            
            // Then add a new job
            s.AddWorkItem(item2);

            Thread.Sleep(100);

            // The 2nd item we added should NOT have been processed.
            Assert.IsFalse(item2.Status == Enumerations.WorkItemStatus.Complete);
        }
    }
}
